/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.info.core;

import java.util.List;
import java.util.Map;

import com.mttang.framework.core.paginated.util.PagedFliper;
import com.mttang.framework.core.pojos.ServiceRequest;

/**
 * 类ServiceRequestAnalyser.java的实现描述：web请求分析
 * 
 * @author caizhan 2010-6-3 下午06:38:12
 */
public class ServiceRequestAnalyser {

    /**
     * 分页对象的key
     */
    public static final String K_pageModel = "pageModel";
    /**
     * 查询条件的key
     */
    public static final String K_parameter = "parameter";
    /**
     * 执行函数的key
     */
    public static final String K_method    = "method";
    /**
     * 结果集的key
     */
    public static final String K_list      = "list";
    /**
     * 查询参数
     */
    private ParameterModel     parameterModel;
    /**
     * 函数
     */
    private String             method;
    /**
     * 输入的结果集
     */
    private DTOModel           dtoModel;

    private ServiceRequest     serviceRequest;
    /**
     * web的请求参数
     */
    private Map                requestObject;

    public ServiceRequestAnalyser(ServiceRequest serviceRequest) {
        super();
        this.parameterModel = new ParameterModel();
        this.serviceRequest = serviceRequest;
        this.requestObject = (Map) this.serviceRequest.getCurrentRequestObject();
    }

    /**
     * 解析web参数为查询参数
     * 
     * @return the parameterModel
     */
    public ParameterModel analyseParmeterModel() {
        // Map pageModelObj = (Map) requestObject.get(PageModel.K_pageModel);
        // PageModel pageModel = new PageModel();
        // Object obj = null;
        // if (pageModelObj != null) {
        // if ((obj = pageModelObj.get(PageModel.K_currentPage)) != null) {
        // pageModel.setCurrentPage(((Number) obj).intValue());
        // }
        // if ((obj = pageModelObj.get(PageModel.K_pageNum)) != null) {
        // pageModel.setPageNum(((Number) obj).intValue());
        // }
        // if ((obj = pageModelObj.get(PageModel.K_pageSize)) != null) {
        // pageModel.setPageSize(((Number) obj).longValue());
        // }
        // }
        // 由对象获取
        PagedFliper pagedFliper = (PagedFliper) requestObject.get(ServiceRequestAnalyser.K_pageModel);
        this.parameterModel.setPagedFliper(pagedFliper);

        Map queryMap = (Map) requestObject.get(K_parameter);
        this.parameterModel.setPramater(queryMap);

        return this.parameterModel;
    }

    /**
     * 解析web参数为操作函数
     * 
     * @return the method
     */
    public String analyseMethod() {
        method = (String) this.serviceRequest.getParameter(K_method);
        return method;
    }

    /**
     * 解析web参数为DTO结果集
     * 
     * @return the model
     */
    public DTOModel analyseModel() {
        List<Map> list = (List<Map>) requestObject.get(K_list);
        dtoModel = new DTOModel(list);
        return dtoModel;
    }

}
